package cn.xz.gobang

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import cn.xz.gobang.activity.*
import cn.xz.gobang.base.BaseActivity
import cn.xz.gobang.bean.CommonLib
import cn.xz.gobang.bean.GoBangUser
import cn.xz.gobang.bean.GoBangUserData
import cn.xz.gobang.bean.UserData
import cn.xz.gobang.databinding.ActivityMainBinding
import cn.xz.gobang.net.OkHttpUtil
import cn.xz.gobang.util.*
import okhttp3.FormBody
import okhttp3.RequestBody
import java.util.ArrayList

/**
 * 首页
 * JsonUtil.serializeAsString(data)
 * */
class MainActivity : BaseActivity<ActivityMainBinding>(R.layout.activity_main) {

    override fun initData() {
        requestPermission()
        //获取用户信息
        initGetUser()
    }

    override fun initEvent() {
        //多人对局
        mDataBind.tvOline.setOnClickListener {
            ARouter.to(MoreActivity::class.java)
        }
        //人机对战
        mDataBind.tvMore.setOnClickListener {
            //ARouter.to(DoubleWelActivity::class.java)
            ARouter.to(RoBotActivity::class.java)
        }
        //棋盘
        mDataBind.tvMn.setOnClickListener {
            ARouter.to(MnActivity::class.java)
        }
        //玩法说明
        mDataBind.tvSm.setOnClickListener {
            ARouter.to(PlayingActivity::class.java)
        }
        //我的
        mDataBind.tvMine.setOnClickListener {
            ARouter.to(MineActivity::class.java)
        }
        //设置
        mDataBind.tvSetting.setOnClickListener {
            ARouter.to(SettingActivity::class.java)
        }
    }

    private fun initGetUser(){
        //获取当前用户ip
        CommonLib.UserIP = AppHelper.getLocalIpAddress()
    }


    /*动态申请权限操作*/
    private var isPermissionRequested = false
    private fun requestPermission() {
        if (Build.VERSION.SDK_INT >= 23 && !isPermissionRequested) {
            isPermissionRequested = true
            val permissionsList: ArrayList<String> = ArrayList()
            val permissions = arrayOf<String>(
                //在这里加入你要使用的权限
                Manifest.permission.READ_CONTACTS,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.READ_CALENDAR,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.WRITE_CONTACTS,
                Manifest.permission.ACCESS_WIFI_STATE,
            )
            for (perm in permissions) {
                if (PackageManager.PERMISSION_GRANTED != checkSelfPermission(perm)) {
                    permissionsList.add(perm)
                    // 进入这里代表没有权限
                }
            }
            if (permissionsList.isNotEmpty()) {
                val strings = arrayOfNulls<String>(permissionsList.size)
                requestPermissions(permissionsList.toArray(strings), 0)
            }
        }
    }
}